'''
Created on Apr 19, 2009

@author: Administrator
'''
from django.contrib import auth
from django.forms import forms
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from com.ericyang.common.web import redirect, page404, response
from com.ericyang.common.web.middleware import threadlocals
from com.ericyang.groupshopping.product.models import  Category, Product, correct_level
from com.ericyang.groupshopping.product.forms import CategoryCreationForm, CategoryChangeForm, ProductCreationForm, ProductChangeForm

# index product
@login_required
def index_product(request):
    level = correct_level(-1)
    user = threadlocals.get_current_user()
    if user.is_superuser:
        return view_category(request, level)
    categories = Category.objects.filter(level=level)
    return response('product/product_index.html', {'categories':categories})

def list_product(request, category_id):
    return response('product/product_list.html', {'products':Product.objects.filter(category__id=category_id)})

# view product
@login_required
def view_product(request, product_id=1):
    return response('product/product_view.html',
                    {'product':Product.objects.get(pk=product_id)})
# create product
@login_required
def create_product(request):
    user = threadlocals.get_current_user()
    if not user.is_company_user():
        return page404()
    if request.method == 'POST':
        form = ProductCreationForm(request.POST, request.FILES)
        if form.is_valid():
            product = form.save()
            return view_product(request, product.id)
    else :
        form = ProductCreationForm()
    
    return response('product/product_edit.html', {'form':form})

# edit a product
@login_required
def change_product(request, product_id):
    user = threadlocals.get_current_user()
    if not user.is_company_user():
        return page404()
    product = get_object_or_404(Product, pk=product_id)
    if request.method == 'POST':
        form = ProductChangeForm(product, request.POST)
        if form.is_valid():
            product = form.save()
            return view_product(request, product.id)
    else :
        form = ProductChangeForm(product)
    
    return response('product/product_edit.html', {'form':form})

# delete a product
@login_required
def delete_product(request, product_id):
    user = threadlocals.get_current_user()
    if not user.is_company_user():
        return page404()
    product = get_object_or_404(Product, pk=product_id)     
    category_id = product.category.id
    if product.is_changable() :
        product.delete()
    return list_product(request, category_id)

# view sub categories
@login_required
def view_sub_categories(request, category_id):
    user = threadlocals.get_current_user()
    #category = get_object_or_404(Category, pk=category_id)
    return response('product/category_sub_list.html',
                    {'categories':Category.objects.filter(super_category__id=category_id),
                     })
# view categories
@login_required
def view_category(request, _level=1):
    user = threadlocals.get_current_user()
    if not user.is_superuser:
        return page404()
    level = correct_level(_level)
    return response('product/category_list.html',
                    {'categories':Category.objects.filter(level=level),
                     'level':level
                     })
# create category
@login_required
def create_category(request, _level=1):
    user = threadlocals.get_current_user()
    if not user.is_superuser:
        return page404()
    level = correct_level(_level)
    if request.method == 'POST':
        form = CategoryCreationForm(level, request.POST)
        if form.is_valid():
            product = form.save()
            return view_category(request, level)
    else :
        form = CategoryCreationForm(level)
    
    return response('product/category_edit.html',
                    {'form':form, 'level':level})

# edit a category
@login_required
def change_category(request, category_id):
    user = threadlocals.get_current_user()
    if not user.is_superuser:
        return page404()
    category = get_object_or_404(Category, pk=category_id)
    level = category.level
    if request.method == 'POST':
        form = CategoryChangeForm(category, request.POST)
        if form.is_valid():
            product = form.save()
            return view_category(request, level)
    else :
        form = CategoryChangeForm(category)
    
    return response('product/category_edit.html',
                    {'form':form, 'level':level})

def delete_category(request, category_id):
    user = threadlocals.get_current_user()
    if not user.is_superuser:
        return page404()
    category = get_object_or_404(Category, pk=category_id)
    level = category.level
    category.delete()
    return view_category(request, level)

